package com.urbo.android.shortcutter;


import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListSelectionDialog {
	private AlertDialog listDialog;			// Dialog to show
	private View listDialogLayout;			// Reference to view hosted in the listDialog
	private ListView thisListView;
	
	private Context context;
	private List<Map<String, String>> listOfIconSources; //List of icon sources. Each item is map of 3 keys. Identifier, Title and Type
	
	// Click listener. Listener wrapper for ListView OnItemClick of this dialog
	private OnClickListener onClickListener = null;
	
	// Interface for ListSelectionDialog.OnClickListener
	public interface OnClickListener {
		public abstract void onClick(Map<String, String> selectedItem);
	}
	
	// OnClickListener setter
	public void SetOnClickListener(OnClickListener listener) {
		onClickListener = listener;
	}
	
	// Internal click called from ListView OnItemClickListener of this dialog. Call ListSelectionDialog.OnClickListener
	private void click(int numberOfSelectedItem){
		if (onClickListener != null) {
			onClickListener.onClick(listOfIconSources.get(numberOfSelectedItem));
		}
	}
	
	// Attach List of items to ListView 
	private void attachAdapter() {
		// Attach adapter to listView
		if (thisListView.getAdapter() != null)
			thisListView.setAdapter(null);
		
		SimpleAdapter listViewAdapter = new SimpleAdapter(context, listOfIconSources, R.layout.list_dialog_item, new String[]{"title"},  new int[]{R.id.list_dialog_item});
		thisListView.setAdapter(listViewAdapter);
	}
	
	// Handler for click on item of ListView
	public AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg, View view, int position,long id) {
			click(position);
		}
	};
	
	
	//public ListSelectionDialog(Context context, ListView.OnClickListener dialogClickListener){
	public ListSelectionDialog(Context context){
		this.context = context;
		createListDialog();
		thisListView = (ListView)listDialogLayout.findViewById(R.id.list_icon_sources);
		thisListView.setOnItemClickListener(onItemClickListener);
	}
	
	// Internal dialog builder
	private void createListDialog(){//DialogInterface.OnClickListener dialogClickListener) {
		AlertDialog.Builder builder;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listDialogLayout = inflater.inflate(R.layout.list_dialog, null);
		builder = new AlertDialog.Builder(context);
		builder.setView(listDialogLayout);
		listDialog = builder.create();
	}
		
	public void setDataSource(List<Map<String, String>> listOfItems) {
		this.listOfIconSources = listOfItems;
		attachAdapter();
	}
	
	public void showReload(List<Map<String, String>> listOfItems) {
		setDataSource(listOfItems);
		listDialog.show();
	}
	
	public void show() {
		listDialog.show();
	}
	
	public void dismiss() {
		listDialog.dismiss();
	}
}
